﻿using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using PropertyManagementSystemAPI.Models;

namespace PropertyManagementSystemAPI.Controllers
{
    /// <summary>
    /// 收费
    /// </summary>
    [Route("api/[controller]")]
    [ApiController]
/*    [EnableCors("any")]*/
    [ApiExplorerSettings(GroupName = "物业")]
    public class TollDetailsController : ControllerBase
    {
        public readonly IFreeSql db;
        public TollDetailsController(IFreeSql _db) 
        {
         db = _db;
        }
        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="ys"></param>
        /// <param name="ym"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        [HttpGet]
        public object ToGet(int ys, int ym, string? name) 
        {
            int pageSize = ys;
            int pageIndex=ym;
            var ww=db.Select<TollDetails, Userinfo,TollItems>()
                .LeftJoin((a,b,c)=>a.DUserId==b.UserId)
                .LeftJoin((a, b, c) => a.DTollId == c.TollId)
               .WhereIf(!string.IsNullOrEmpty(name), (a, b, c) => b.UserName == name)
               .ToList((a, b,c) => new { a.DId, a.DMoney, a.DPayState, a.DPayTime, a.DCreateTime, a.DRemark, a.DTollId, b.UserName,c.TollName });
            int totalCount=ww.Count();
            var list=ww.Skip((pageIndex-1)*pageSize).Take(pageSize).ToList();
            return new
            { 
               TotalCount=totalCount,
               Data=list
            };
        }
       

        [HttpPost]
        public int Add(TollDetails cellinfo)
        {
            var cell = new TollDetails()
            {
                DId = cellinfo.DId,
                DTollId = cellinfo.DTollId,
                DUserId = cellinfo.DUserId,
                DCreateTime = cellinfo.DCreateTime,
                DMoney = cellinfo.DMoney,
                DPayState = cellinfo.DPayState,
                DPayTime = cellinfo.DPayTime,
                DRemark = cellinfo.DRemark,
               
            };
            var query = db.Insert(cell).ExecuteAffrows();
            return query;
        }
        [HttpPut]
        public int Update(int id, TollDetails cellinfo)
        {
            var cell = db.Update<TollDetails>().Set(a => new TollDetails
            {
                DId = cellinfo.DId,
                DTollId = cellinfo.DTollId,
                DUserId = cellinfo.DUserId,
                DCreateTime = cellinfo.DCreateTime,
                DMoney = cellinfo.DMoney,
                DPayState = cellinfo.DPayState,
                DPayTime = cellinfo.DPayTime,
                DRemark = cellinfo.DRemark,
            }).Where(a => a.DId == id).ExecuteAffrows();
            return cell;
        }
        [HttpDelete]
        public int Delete(int id)
        {
            var affectedRows = db.Delete<TollDetails>()
                .Where(a => a.DId == id)
                .ExecuteAffrows(); //id删除
            return affectedRows;
        }

        //[HttpGet]
        //public object Tolldw()
        //{
        //    var hh = db.Select<TollItems>().ToList(
        //    (a) => new { a.TollId, a.TollName });
        //    return new
        //    {
        //        Data = hh
        //    };
        //}
    }
}
